package com.kompas.bola.application;

import java.util.HashMap;

import com.kompas.bola.application.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings.ZoomDensity;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.Fields;
import com.kompas.bola.application.analytics.FlurryUtil;
import com.kompas.bola.application.analytics.GAnalyticsUtil;
import com.kompas.bola.application.db.CrudHelper;
import com.kompas.bola.application.utils.Constants;
/** =============================================================================== */
public class WebActivity extends Activity {
	String url;
	private WebView webView;
	private WebView imgProgressBar;
	private HashMap<String, String> value;
	/** =========================================================================== */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.web_screen);		
		overridePendingTransition(R.anim.slide_in_from_right, R.anim.slide_out_to_left);
		
		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setHomeButtonEnabled(true);
		getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffffff")));
		getActionBar().setLogo(getResources().getDrawable(R.drawable.logo));
		getActionBar().setTitle(Constants.VIEW_WEB_SCREEN);

		/** 
		 * START GOOGLE ANALYTICS */
		value													= new HashMap<String, String>();
		value.put(Fields.HIT_TYPE, "appview");
		value.put(Fields.SCREEN_NAME, Constants.VIEW_WEB_SCREEN);
		GAnalyticsUtil.startSessionWithGA(this,value);

		Intent in 							= getIntent();
		url									= in.getStringExtra(Constants.URL);
		webView								= (WebView) findViewById(R.id.web_view);
		imgProgressBar						= (WebView) findViewById(R.id.imgProgressBarWeb);		

		String path							= "file:///android_asset/loading.html";
		imgProgressBar.loadUrl(path);	
		imgProgressBar.getSettings().setDefaultZoom(ZoomDensity.FAR);
		imgProgressBar.setVisibility(View.VISIBLE);
		
		webView.getSettings().setDomStorageEnabled(true);
		webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
		webView.getSettings().setAllowFileAccess(true);
		webView.getSettings().setBlockNetworkLoads(false);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
		webView.getSettings().setSupportMultipleWindows(true);
		webView.getSettings().setSupportZoom(true);
		webView.getSettings().setBuiltInZoomControls(true);
		webView.getSettings().setUseWideViewPort(true);
		webView.setWebChromeClient(new WebChromeClient(){});
		
		webView.setWebViewClient(new WebViewClient(){
			public boolean shouldOverrideUrlLoading(WebView view, String url){
				return false;				
		    }
		});

		/*imgProgressBar.setVisibility(View.GONE);*/
		webView.loadUrl(url);
		webView.setVisibility(View.VISIBLE);
	}
	/** =========================================================================== */
	public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
        	Intent main						= new Intent(WebActivity.this, MainActivity.class);
            startActivity(main);
			overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_to_right);
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }	
	/** =========================================================================== */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case android.R.id.home:
        	Intent main						= new Intent(WebActivity.this, MainActivity.class);
            startActivity(main);
			overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_to_right);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	/** =========================== s : Google Analytics ========================== */
	@Override
	public void onStart() {
		super.onStart();			

		EasyTracker.getInstance(this).activityStart(this);  // Add this method.
	    FlurryUtil.startSessionWithFlurry(this, Constants.VIEW_WEB_SCREEN);
	}
	/** =========================================================================== */
	@Override
	public void onStop() {
		super.onStop();

	    EasyTracker.getInstance(this).activityStop(this);  // Add this method.
	    FlurryUtil.endSessionWithFlurry(this,  Constants.VIEW_WEB_SCREEN);
	}
	/** =========================== e : Google Analytics ========================== */
}
